#include "gsquare.h"

GSquare::GSquare(QWidget *parent) :
    QWidget(parent)
{
    scene = NULL;
    fond = NULL;
    vw_square = NULL;
    nb_col = 19;
    j1 = NULL;
    j2 = NULL;
    current_player = 0;
}

GSquare::~GSquare()
{
    if (scene != NULL)
        delete scene;
    if (fond != NULL)
        delete fond;
    if (vw_square != NULL)
        delete vw_square;
}

const void GSquare::createMySquare()
{
    // Window
    this->setMinimumSize(800, 600);
    this->setMaximumSize(800, 600);

    // Creer la scene
    scene = new QGraphicsScene(this);


    // Creer le fond
    QPixmap pix = QPixmap("fond.png", "PNG");
    fond = new QGraphicsPixmapItem(pix, NULL, scene);

    //  qreal scale = (qreal)((800.0 - pix.size().height()) / /*(pix.size().width())*/ 1000);
    qreal scale = 0.62;
    fond->setScale(scale);


    // Creer la vue
    this->vw_square = new QGraphicsView(scene);
    this->vw_square->setBaseSize(900, 700);
    this->vw_square->setMaximumSize(900, 700);
    this->vw_square->setMinimumSize(900, 700);

    QTransform matrice;
 //   matrice.rotateRadians(-40, Qt::XAxis);
//    matrice.translate(100, 100,     );
   // matrice.scale(1.2, 1.2);
    this->vw_square->setTransform(matrice, true);
    this->vw_square->show();

    // Creer les bouttons + Case
    group_pions = new QButtonGroup(this);
    for (int y = 0; y < nb_col; ++y)
    {
        for (int x = 0; x < nb_col; ++x)
        {
            // Creer la case
            if (x < nb_col - 1 && y < nb_col - 1)
            {
                QPixmap tmp_pix("case.png");
                QGraphicsPixmapItem *pix = new QGraphicsPixmapItem(tmp_pix, fond, scene);
                pix->setPos( 40 + x * 50, 40 + y * 50);
                pix->scale(1.1, 1.1);
            }

            // Creer le Bouton
            GCase *btn = new GCase(NULL, x, y);
            btn->setGeometry(20 + x * 50, 20 + y * 50, 50, 50);
            btn->setFlat(true);
            btn->setAutoFillBackground(false);
            btn->setTaken(false);

            group_pions->addButton(btn, x * 100 + y);
            QObject::connect(group_pions, SIGNAL(buttonPressed(QAbstractButton*)), this, SLOT(button_pressed(QAbstractButton *)));
            QObject::connect(group_pions, SIGNAL(buttonReleased(QAbstractButton*)), this, SLOT(button_released(QAbstractButton *)));

            QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(fond);
            proxy->setWidget(btn);
            proxy->setPalette(QPalette(QColor(0, 0, 0, 0)));
        }
    }

}

const void GSquare::createPlayers()
{
    j1 = new PlayerManager(true);
    j2 = new PlayerManager(true);
}



const void GSquare::setFond(QPixmap *pixmap)
{
    if (fond != NULL)
    {
        scene->removeItem(fond);
        delete fond;
    }
    fond = scene->addPixmap(*pixmap);
    vw_square->update();
}

void GSquare::check_result(int result)
{
    return;
}

void GSquare::change_currentPlayer()
{
    if (current_player == 0)
        ++current_player;
    else
        --current_player;
}



void GSquare::change_btn_state(GCase *tmp_btn)
{
    QIcon ico;
    if (current_player == 0)
    {
        QPixmap pix("pion_rouge.png");
        pix.scaled(2, 2);
        ico.addPixmap(pix);
    }
    else
    {
        QPixmap pix("pion_noir.png");
        pix.scaled(2, 2);
        ico.addPixmap(pix);
    }
    ico.actualSize(QSize(tmp_btn->size().width() *2, tmp_btn->size().height() + 2));
    tmp_btn->setIcon(ico);
    tmp_btn->setIconSize(QSize(tmp_btn->size().width() , tmp_btn->size().height() ));
    tmp_btn->setAutoFillBackground(false);
    tmp_btn->setTaken(true);
    //    tmp_btn->setEnabled(false);
}


void GSquare::button_released(QAbstractButton *sender)
{
    GCase *tmp_btn = (GCase *)sender;

    // TODO récupérer le resultat de l'ia
    JudgeDredd judge;
    coord_t coord;
    coord.x = tmp_btn->getX();
    coord.y = tmp_btn->getY();

    GameInfo info;

    try
    {
        judge.putPieceOnMap(info, coord);
    }
    catch (GameExceptionWinByScore e)
    {
        // TODO animate victory
        QLabel label(this);
        label.setGeometry(19 * 40 + 120, 40 * 2, 200, 50);
        label.setText("You win by score");
        //label.palette(QPalette(QColor(255, 255, 255, 255)));
        label.show();
    }
    catch (GameExceptionWinByAlignement e)
    {
        // TODO animate Victory
        QLabel label(this);
        label.setGeometry(19 * 40 + 120, 40 * 2, 200, 50);
        label.setText("You win by alignement");
        //label.palette(QPalette(QColor(255, 255, 255, 255)));
        label.show();
    }
    catch (GameExceptionTwinTriple e)
    {
        box.setText("Vous ne pouvez pas effectuer ce coup");
        box.setWindowTitle("Coup Interdit");
        box.exec();
        QTimer::singleShot(300, this, SLOT(end_of_box()));
    }
    catch (InvalidMapException e)
    {
//        box(this);
        box.setText("Exception InvalidMap");
        box.setWindowTitle("Coup Interdit");
        box.exec();
        QTimer::singleShot(300, this, SLOT(end_of_box()));
    }
    catch (GameInfo e)
    {
        //box(this);
        box.setText("Exception GameInfo");
        box.setWindowTitle("Coup Interdit");
        box.exec();
        QTimer::singleShot(300, this, SLOT(end_of_box()));
    }
    //afficher le résultat
    if (tmp_btn->isTaken() == false)
        change_btn_state(tmp_btn);

//
    change_currentPlayer();

}

void GSquare::end_of_box()
{
    box.close();
}


void GSquare::button_pressed(QAbstractButton *sender)
{
//    tmp_btn->backgroundRole()
}

